package array;

import java.util.Arrays;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/8/23 10:09
 * 数组练习
 * 1.将数组转化为字符串
 *      不能直接打印
 *      采用 Arrays.toString()方法 打印数组 自己实现
 * 2.数组全拷贝
 *       采用Arrays.copyOf(arr,arr.length+1)  自己实现
 * 3.在数组中查找元素，返回下标
 *       public static int find(int[] arr,int toFind)
 * 4.二分查找：适用于有序的数组，升序或降序
 *       递归和非递归实现
 * 5.数组逆序
 * 6.冒泡排序 及其优化
 *
 */
public class ArrayPractice {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        int[] arr1={1,2,9,3,4,2,6,22,33,111,22222,32,4,324,432,142,34,2212};
        /*int[] arr1 = Arrays.copyOf(arr,arr.length+1);
        int[] arr2 = Arrays.copyOfRange(arr,2,4);
        System.out.println(dBinaryFind(arr,4,0,arr.length-1));*/
        bubbleSort(arr1);
        reserveArray(arr);
        System.out.println(Arrays.toString(arr1));
//        System.out.println(Arrays.toString(arr1));
//        System.out.println(Arrays.toString(arr2));
    }
    // 自己实现类似JDK的数组转字符串操作,实际上就是遍历数组，通过字符串的拼接操作而已
    public static String arr2Str(int[] arr) {
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            // 什么时候才需要加,分隔，最起码当前遍历的元素之后还有元素，才需要分隔
            // 此时i还没有走到最后一个数组元素
            if (i < arr.length - 1) {
                ret += ", ";
            }
        }
        ret += "]";
        return ret;
    }
    //类似Arrays.copyOf(arr,arr.length+1)
    public static int[] copyOf(int[] arr) {
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }


    public static int find(int[] arr,int toFind){
        for (int i = 0; i < arr.length; i++) {
            if(toFind==arr[i]){
                return i;
            }
        }
        return -1;
    }
    public static int binaryFind(int[] arr,int toFind){

        int left = 0;
        int right = arr.length-1;
        while(left<=right){
//            int mid = (right+left)/2;
            int mid = left + ((right-left)>>1);
            if(arr[mid]==toFind){
                return mid;
            }else if(arr[mid]<toFind){
                left=mid+1;
            }else {
                right =mid-1;
            }
        }
        return -1;
    }
    public static int dBinaryFind(int[] arr,int toFind,int left,int right){
        if(left>right){
            return -1;
        }
        int mid=left + ((right-left)>>1);
        if(arr[mid]==toFind){
            return  mid;
        }else if(arr[mid]<toFind){
            return dBinaryFind(arr,toFind,mid+1,right);
        }else {
            return dBinaryFind(arr,toFind,left,mid-1);
        }
    }
    public static void reserveArray(int[] arr){
        int tem ;
        int left = 0;
        int right=arr.length-1;
        while (left<=right){
            tem = arr[left];
            arr[left] = arr[right];
            arr[right]=tem;
            left++;
            right--;
        }
    }
    public static void bubbleSort(int[] arr){
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            //假设有序
            boolean flag = true;
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]){
                    //进入则无顺序
                    flag= false;
                    int tem = arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tem;
                }
            }
            System.out.println(count++);
            //若没进入无序，则本身有序，退出循环
            if (flag==true){
                break;
            }
        }
    }
}
